package com.atkxp.service.impl;

import com.atkxp.dao.BookDao;
import com.atkxp.dao.impl.BookDaoImpl;
import com.atkxp.domain.Book;
import com.atkxp.domain.Page;
import com.atkxp.service.BookService;

import java.util.List;

/**
 * @author user
 * @declaration
 * @create 2022/9/25-18:15
 */
public class BookServiceImpl implements BookService {

    private BookDao bookDao = new BookDaoImpl();
    @Override
    public int addBook(Book book) {
        return bookDao.addBook(book);
    }

    @Override
    public int deleteBookById(int id) {
        return bookDao.deleteBookById(id);
    }

    @Override
    public int updateBook(Book book) {
        return bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(int id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBooks() {
        return bookDao.queryBooks();
    }

    @Override
    public Page<Book> page(int pageNum, int pageSize) {
        Page<Book> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  bookDao.queryBookForPageTotalCount();
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        List<Book> items = bookDao.queryForPageItems(begin, pageSize);
        page.setItem(items);
        return page;
    }

    @Override
    public Page<Book> pageByPrice(int pageNum, int pageSize, int min, int max) {
        Page<Book> page = new Page<>();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        Long cnt1 =  bookDao.queryBookForPageTotalCountByPrice(min, max);
        long cnt2 = cnt1;
        int bookForPageTotalCount = (int)cnt2;
        page.setPageTotalCount((int)(bookForPageTotalCount));
        page.setPageTotalPage((bookForPageTotalCount % pageSize == 0 ? bookForPageTotalCount / pageSize : bookForPageTotalCount / pageSize + 1));
        int begin = (pageNum - 1) * pageSize;
        List<Book> items = bookDao.queryForPageItemsByPrice(begin, pageSize, min, max);
        page.setItem(items);
        return page;
    }
}
